/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.core.action;

import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import org.kommando.core.catalog.CatalogObject;

/**
 * Partial abstract implementation of {@link Action}.
 * <p>
 * Normally, this class should be used when implementing a new {@link Action}.
 * 
 * @author Peter De Bruycker
 */
public abstract class AbstractAction implements Action {

    // TODO attribute icon: taken from tango set -> applications-system.svg
    private static final Icon DEFAULT_ICON = new ImageIcon(AbstractAction.class.getResource("/icons/action.png"));

    @Override
    public Icon getIcon() {
        return DEFAULT_ICON;
    }

    @Override
    public List<CatalogObject> execute(List<CatalogObject> directObjects, List<CatalogObject> indirectObjects)
            throws ActionExecutionException {
        List<CatalogObject> results = new ArrayList<CatalogObject>();

        for (CatalogObject direct : directObjects) {
            if (indirectObjects.isEmpty()) {
                CatalogObject result = execute(direct, null);
                if (result != null) {
                    results.add(result);
                }
            } else {
                for (CatalogObject indirect : indirectObjects) {
                    CatalogObject result = execute(direct, indirect);
                    if (result != null) {
                        results.add(result);
                    }
                }
            }
        }

        return results;
    }

    public CatalogObject execute(CatalogObject direct, CatalogObject indirect) throws ActionExecutionException {
        throw new UnsupportedOperationException("Override one of the execute methods of AbstractAction");
    }

    @Override
    public boolean isApplicable(CatalogObject direct) {
        Class<?>[] directExtensionTypes = getDirectExtensionTypes();

        for (Class<?> extensionType : directExtensionTypes) {
            if (direct.hasExtension(extensionType)) {
                return true;
            }
        }

        return false;
    }

    @Override
    public Class<?>[] getIndirectTypes() {
        return null;
    }

    @Override
    public boolean isIndirectMandatory(CatalogObject direct) {
        return false;
    }

    @Override
    public boolean isIndirectOptional(CatalogObject direct) {
        return false;
    }

    @Override
    public CatalogObject getDefaultIndirectObjectValue(CatalogObject directObject) {
        return null;
    }
}
